{* $Id$ *}
{include_core file="admin/lib/header.html" title="积分记录添加"}
<div class="page-header border-bottom clearfix">
  <h2 class="left">积分记录管理<span>为用户增加积分</span></h2>
  <a href="{AnUrl('score_record/admin/index')}" class="btn btn-default btn-sm right">&lt;&nbsp;返回积分记录列表</a>
</div>
<!--start:积分记录新增-->
<div class="add-form">
  <form action="{AnUrl('score_record/admin/create')}" method="post" enctype="multipart/form-data">
    <input type="hidden" name="score[user_id]" value="1">
    <input type="hidden" name="score[store_id]" value="1">
    <table class="table" cellpadding="0" cellspacing="0" style="position:relative;">
      <tr>
        <th><label class="add-f-label" for="">积分类型：</label></th>
        <td>
          <select name="score[res_id]" id="">
            {foreach $score_type_list as $k=>$v}
              <option value="{$v.id}">{$v.res_name}</option>
              {if $v.type_list}
                {foreach $v.type_list as $key=>$val}
                <option value="{$val.id}">&nbsp;&nbsp;|--&nbsp;&nbsp;{$val.res_name}</option>
                {/foreach}
              {/if}
            {/foreach}
          </select>
          <span class="btn btn-default btn-sm">
            <a href="{AnUrl('score_type/admin/add')}">&nbsp;&nbsp;没找到类型? 点击添加</a>
          </span>
        </td>
      </tr>
      <tr style="position:relative;">
        <th><label for="" class="add-f-label">用户名：</label></th>
        <td style="position:relative;">
          <input name="score[user_name]" style="width:490px;" type="text" class="add-f-ipt1" maxlength="50" placeholder="输入用户名" >
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>输入用户名</div>
          <ul class="js_ajaxUserList" style="display:none;position: absolute; top:40px;background:#fff;width: 495px;border: 1px #ccc solid;max-height:320px;overflow-y:auto;overflow-x:hidden;">
            
          </ul>
          <ul style="display:none;" class="js_tempUl">
            <li class="js_firstLi" style="display:none;width:490px;height:34px;line-height:34px;color: #f00;border-bottom:1px solid #ccc;text-indent:10px;">asdfad</li>
          </ul>
        </td>
      </tr>
      <tr>
        <th><label for="" class="add-f-label">旗下店铺：</label></th>
        <td>
          <!-- <input name="score[store_name]" style="width:490px;" type="text" class="add-f-ipt1" maxlength="50" placeholder="输入店铺名" > -->
          <div name="score[store_name]" style="width:490px;height:30px;border:1px solid #ccc;line-height:30px;text-indent:10px;"></div>
          <!-- <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>输入用户名</div>
          <ul class="js_ajaxStoreList" style="display:none;position: absolute; top:40px;background:#fff;width: 495px;border: 1px #ccc solid;max-height:320px;overflow-y:scroll;overflow-x:hidden;">
            
          </ul>
          <ul style="display:none;" class="js_tempStoreUl">
            <li class="js_firstStoreLi" style="display:none;width:490px;height:34px;line-height:34px;color: #f00;border-bottom:1px solid #ccc;text-indent:10px;"> </li>
          </ul> -->
        </td>
      </tr>
      <tr>
        <th valign="top"><label class="add-f-label" for="">积分：</label></th>
        <td>
          <input name="score[score]" style="width:490px;" type="text" class="add-f-ipt1" maxlength="50" placeholder="输入积分,只能是整数" >
          <div class="tips"><span style="line-height: 34px;color: #f00;margin-right: 10px;">*</span>输入要添加的积分数</div>
        </td>
      </tr>
      
      <tr><th>&nbsp;</th><td class="noborder"><button type="submit" class="btn btn-success">&nbsp;&nbsp;&nbsp;添加&nbsp;&nbsp;&nbsp;</button></td></tr>
    </table>
  </form>
</div>
<!--end:积分记录新增-->
<script>

$(function(){ 
  //控制ajax的同步还是异步
  var ajaxAsync = true;   
  var checkUserUnique = false;
  //初始化清空用户名的值
  $('input').val('');

  // 提交验证
  $('form').submit(function(){
    //验证
    if($('input[name="score[user_name]"]').val() == '' || $('input[name="score[score]"]').val() == ''){
      showMsg('用户名或积分数不能为空!');
      return false;
    };

    //ajax发送验证
    //获取用户名
    var user_name = $.trim( $('input[name="score[user_name]"]').val() );
    //过滤空字串,过短字符串及空格字符,判断是否和原来的值相同,减少请求次数
    if ( !$.trim(username) ) {
      $('.js_ajaxUserList').slideUp('fast');
      return false;
    };

    ajaxAsync = false;
    $('input[name="score[user_name]"]').keyUp();
    ajaxAsync = true;

    if (!checkUserUnique) {
      showMsg('用户名不唯一');
      return false;
    };

    if (!checkStoreUnique) {
      showMsg('店铺不唯一');
      return false;
    };

  });

  //积分除了数字之外的字符过滤
  {literal}
  $('input[name="score[score]"]').keyup(function(){
    var reg = /[^-+0123456789]+/g;
    $(this).val($(this).val().replace(reg, ''));
    
    //获取第一个字符
    $firstLetter = $(this).val().substr(0, 1);
    var signReg = /\D+/g;
    //获取剩余字符过滤
    $leftLetter = $(this).val().substr(1);
    $leftLetter = $leftLetter.replace(signReg, '');
    //最后拼接
    $(this).val($firstLetter+$leftLetter);
    
  });
  {/literal}
  
  /* $('input[name="score[user_name]"]').blur(function(){
	  $('.js_ajaxUserList').slideUp('fast');
	  $('.js_ajaxUserList').empty();
  }); */
  
  //ajax查询匹配的用户名
  $('input[name="score[user_name]"]').keyup(function(){

    //过滤空字串,过短字符串及空格字符,判断是否和原来的值相同,减少请求次数
    if ( !$.trim( $(this).val() ) ) {
      $('.js_ajaxUserList').slideUp('fast');
      return false;
    };

    //获取文本框内的值
    var user_name = $.trim( $(this).val() );

    $('.js_ajaxUserList').empty();
    var userMatchList = [];
    //发送ajax
    $.ajax({
      url : 'score_record/admin/ajax_search?user_name='+user_name,
      success : function(res){
       
        if(res == '0'){
          $('.js_ajaxUserList').slideUp();
          return false;
        }
        
        if (res.length == 1) {
          checkUserUnique = true;
          $('input[name="score[user_id]"]').val(res[0]['id']);
          //如果用户名是唯一的就可以获取旗下的店铺列表
          //发送ajax获取旗下的店铺列表
          $.ajax({
            url : 'score_record/admin/ajax_search_store_list?user_id='+res[0]['id'],
            success : function(store_info){

              if (store_info == '0') {
                $('div[name="score[store_name]"]').html('暂无旗下分店');
                return false;
              };

              if (store_info) {
                checkStoreUnique = true;
                $('input[name="score[store_id]"]').html(store_info['id']);
                $('div[name="score[store_name]"]').html(store_info['title']);
                return false;
              }
            },
            dataType : 'json',
            type : 'get',
            async : ajaxAsync
          });

        } else {
          checkUserUnique = false;
        }
        $('.js_ajaxUserList').slideDown();
        $('.js_ajaxUserList').empty();
        for(var i in res){

          var newLi = $('.js_tempUl .js_firstLi').clone(true, true);
          newLi.css('display', 'block');
          newLi.html(res[i]['user_name']);
          newLi.attr('user_id', res[i]['id']);
          $('.js_ajaxUserList').append(newLi);

          //绑定用户列表的鼠标划上事件
          newLi.mouseover(function(){

            $('.js_ajaxUserList li').css('background', 'rgb(255, 255, 255)');
            $(this).css('background', 'rgb(150, 200, 220)');
            $(this).css('cursor', 'pointer');
                  
          });

          newLi.mouseout(function(){
            $(this).css('background', 'rgb(255, 255, 255)');
          });

          newLi.click(function(){
            $('input[name="score[user_name]"]').val($(this).html());
            $('input[name="score[user_id]"]').val($(this).attr('user_id'));
            $('.js_ajaxUserList').slideUp('fast');
            //发送ajax获取旗下的店铺列表
            $.ajax({
              url : 'score_record/admin/ajax_search_store_list?user_id='+res[0]['id'],
              success : function(store_info){

                if (store_info == '0') {
                  $('div[name="score[store_name]"]').html('暂无旗下分店');
                  return false;
                };

                if (store_info) {
                  checkStoreUnique = true;
                  $('input[name="score[store_id]"]').html(store_info['id']);
                  $('div[name="score[store_name]"]').html(store_info['title']);
                  return false;
                }
              },
              dataType : 'json',
              type : 'get',
              async : ajaxAsync
            });

          });
        }
      },
      dataType : 'json',
      type : 'get',
      async : ajaxAsync
    });
  });

  $('.js_ajaxUserList li').mouseover(function(){

    $('.js_ajaxUserList li').css('background', 'rgb(255, 255, 255)');
    $(this).css('background', 'rgb(150, 200, 220)');
    $(this).css('cursor', 'pointer');
          
  });

  $('.js_ajaxUserList li').mouseout(function(){

    $(this).css('background', 'rgb(255, 255, 255)');

  });

  

});

</script>
{include_core file="admin/lib/footer.html"}